import unittest
from random import randint
from selenium import webdriver
from HTMLTestRunner import HTMLTestRunner
from time import sleep

from selenium.webdriver.support.select import Select


class JXC_Test(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.get('http://localhost/jxc/index.asp')

    def test_login(self):
        self.driver.find_element_by_name('username').send_keys('admin')
        self.driver.find_element_by_name('pwd').send_keys('admin')
        self.driver.find_element_by_name('enter').click()
        self.assertIn('后台管理系统', self.driver.page_source)

    def test_danwei_delete(self):
        sleep(1)
        self.driver.switch_to.frame('left')
        sySet = '.menuall > tbody:nth-child(1) > tr:nth-child(13) > td:nth-child(1) > img:nth-child(1)'
        self.driver.find_element_by_css_selector(sySet).click()
        sleep(1)
        JlDwSet = '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(7) > td:nth-child(1) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(2)'
        self.driver.find_element_by_css_selector(JlDwSet).click()
        sleep(1)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('right')
        self.driver.find_element_by_name('chkall').click()
        DelSet = 'input.button:nth-child(2)'
        self.driver.find_element_by_css_selector(DelSet).click()
        sleep(2)
        self.driver.switch_to.alert.accept()
        sleep(1)
        self.assertNotIn('name="ID"', self.driver.page_source)

    def test_danwei_add(self):
        bt_set = 'input.button:nth-child(1)'
        self.driver.find_element_by_css_selector(bt_set).click()
        self.driver.find_element_by_name('danwei').send_keys('t')
        sleep(1)
        self.driver.find_element_by_name('submit').click()
        sleep(1)
        self.driver.switch_to.alert.accept()
        sleep(1)
        self.assertIn('t', self.driver.page_source)

    def test_AddDL(self):
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('left')
        dalei_css = '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(5) > td:nth-child(1)'
        self.driver.find_element_by_css_selector(dalei_css).click()

        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('right')

        # bt1_css='input.button:nth-child(1)'
        self.driver.find_element_by_css_selector(self.bt_css).click()

        self.driver.find_element_by_name('bigclass').send_keys('文具')
        self.driver.find_element_by_name('submit').click()
        sleep(2)
        self. driver.switch_to.alert.accept()
        sleep(3)

        self.assertIn('文具', self.driver.page_source)

    def test_AddXL(self):
        self.driver.switch_to.default_content()
        self. driver.switch_to.frame('left')
        xiaolei_css = '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(6) > td:nth-child(1)'
        self.driver.find_element_by_css_selector(xiaolei_css).click()

        self. driver.switch_to.default_content()
        self.driver.switch_to.frame('right')

        # bt_css='input.button:nth-child(1)'
        self. driver.find_element_by_css_selector(self.bt_css).click()

        # 随机生成
        sel_list = Select(self.driver.find_element_by_name('bigclass'))

        lst_count = len(sel_list.options)
        rand = randint(0, lst_count - 1)
        sel_list.select_by_index(rand)
        sleep(3)

        # 添加小类
        self.driver.find_element_by_name('smallclass').send_keys('钢笔')
        self.driver.find_element_by_name('submit').click()
        self.driver.switch_to.alert.accept()
        sleep(3)
        self.assertIn('钢笔', self.driver.page_source)

    def test_SafeQuit(self):
        self.driver.switch_to.default_content()
        # 切换到顶部框架
        self. driver.switch_to.frame('topFrame')
        self.driver.find_element_by_link_text('安全退出').click()
        sleep(1)
        self. driver.switch_to.default_content()
        self.assertIn('系统登陆', self.driver.page_source)

    def test_quit(self):
        self.driver.quit()

def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(JXC_Test('test_login'))
    suite.addTest(JXC_Test('test_danwei_delete'))
    suite.addTest(JXC_Test('test_danwei_add'))
    suite.addTest(JXC_Test('test_AddDL'))
    suite.addTest(JXC_Test('test_AddXL'))
    suite.addTest(JXC_Test('test_SafeQuit'))
    suite.addTest(JXC_Test('test_quit'))
    return suite

with open('C:/专业实习/测试报告/cs2.html', 'wb') as f:
    runner = HTMLTestRunner(stream=f, title='测试报告')
    runner.run(test_suite())